<?php
class PACore{
  const PACORE = 'Core';
  const ERROR_TEMPLATES_DIR = '/Core/templates/errors/';
  const LAYOUTS_DIR = '/Core/templates/';
  const DOCUMENT_TEMPLATES_DIR = '/documents/templates/';
  
  protected $applicationConfiguration;
  protected $applicationsRouting = array();
  
  private static $_instance;
  
  
  static public function getInstance()
  {
    if(self::$_instance == null)
    {
      self::$_instance = new PACore();
      return self::$_instance;
    }
    else return self::$_instance;
  }
  
  public function __construct()
  {
    $this->applicationConfiguration = sfProjectConfiguration::getActive();
  }
  
  public function getApplicationConfiguration()
  {
    return $this->applicationConfiguration;
  }
  
  public function getAppRouting($appName)
  {
    if(!isset($this->appsRouting[$appName]))
    {
      $routing = new sfPatternRouting(new sfEventDispatcher());
      
      $config = new sfRoutingConfigHandler();
      $routes = $config->evaluate(array(sfConfig::get('sf_apps_dir').'/'.$appName.'/config/routing.yml'));
      
      $routing->setRoutes($routes);
      $this->appsRouting[$appName] = $routing;
      return $this->appsRouting[$appName];
    }
    else return $this->appsRouting[$appName];
  }
  
  public function generateAppUrl($appName, $name, $absolute = true, $parameters = array())
  {
    $host = sfContext::getInstance()->getRequest()->getHost();
    if($this->getApplicationConfiguration()->getEnvironment() == 'dev')
    {
      return 'http://'.$host.'/'.$appName.'_dev.php'.$this->getAppRouting($appName)->generate($name, $parameters);
    }
    else
    {
      if($appName != self::PACORE && !$absolute)
      {
        return '/'.$appName.'.php'.$this->getAppRouting($appName)->generate($name, $parameters);
      }
      elseif($appName != self::PACORE)
      {
        return 'http://'.$host.'/'.$appName.'.php'.$this->getAppRouting($appName)->generate($name, $parameters);
      }
      elseif($appName == self::PACORE)
      {
        return 'http://'.$host.'/index.php'.$this->getAppRouting($appName)->generate($name, $parameters);
      }
    }
  }
  
  public function getLoginPage()
  {
    return $this->generateAppUrl('Core', 'login');
  }
  
  public function getSecurePage()
  {
    return $this->generateAppUrl('Core', 'secure');
  }
  
  static public function getStylesheets($file = array(), $exceptFiles = array())
  {
    $stylesheets = "";
    $dir = __DIR__.'/../../web';
    $host = 'http://'.sfContext::getInstance()->getRequest()->getHost();
    $stDir = $dir.'/css/';
    $files = scandir($stDir);
    
    if(count($file) == 0){
      foreach($files as $file)
      {
        if($file != '.' && $file != '..' )//&& $file != 'login.css')
        {
          if(count($exceptFiles))
          {
            foreach($exceptFiles as $except){
              if($file != $except) $stylesheets .= '<link rel="stylesheet" type="text/css" media="screen" href="'.$host.'/css/'.$file.'" />'."\n    ";
            }
          }
          else
          {
              $stylesheets .= '<link rel="stylesheet" type="text/css" media="screen" href="'.$host.'/css/'.$file.'" />'."\n    ";
          }
        }
      }
    }
    elseif(count($file) && !(isset($file['name']) && isset($file['media'])))
    {
      $files = $file;
      foreach($files as $file)
      {
        $stylesheets .= '<link rel="stylesheet" type="text/css" media="screen" href="'.$host.'/css/'.$file.'" />'."\n    ";
      }
    }
    elseif(isset($file['name']) && isset($file['media'])){
      $stylesheets = '<link rel="stylesheet" type="text/css" media="'.$file['media'].'" href="/css/'.$file['name'].'" />'."\n    ";
    }
    return trim($stylesheets, ' ');
  }
  
  static public function getJavascripts()
  {
    $javascripts = "";
    $dir = __DIR__.'/../../web';
    $jsDir = $dir.'/js/';
    $files = scandir($jsDir);
    
    foreach($files as $file)
    {
      if($file != '.' && $file != '..')
      {
        $javascripts .= '<script src="/js/'.$file.'" type="text/javascript"></script>'."\n    ";
      }
    }
    return trim($javascripts, ' ');
  }
  
  static public function loadYml($file)
  {
    $appsDir = sfConfig::get('sf_apps_dir');
    return sfYaml::load($appsDir.'/Core/config/'.$file.'.yml');
  }
  
  
  static public function getPADir()
  {
    return sfConfig::get('sf_apps_dir')."/../lib/ProdAssistant";
  }
  
  static public function getCoreModuleDir()
  {
    return sfConfig::get('sf_apps_dir')."/Core";
  }
  
  static public function getErrorTemplatesDir()
  {
    return sfConfig::get('sf_apps_dir').self::ERROR_TEMPLATES_DIR;
  }
  
  static public function getLayoutsDir()
  {
    return sfConfig::get('sf_apps_dir').self::LAYOUTS_DIR;
  }
  
  static public function getDocumentTemplatesDir()
  {
    return self::getPADir().self::DOCUMENT_TEMPLATES_DIR;
  }
}
